package com.qf.smartplatform.service.impl;

import com.qf.smartplatform.cache.DefaultRoleCache;
import com.qf.smartplatform.cache.RoleCache;
import com.qf.smartplatform.exceptions.AddDataException;
import com.qf.smartplatform.exceptions.MyBaseException;
import com.qf.smartplatform.exceptions.QueryDataException;
import com.qf.smartplatform.exceptions.ResultCode;
import com.qf.smartplatform.mapper.UserMapper;
import com.qf.smartplatform.pojo.CheckType;
import com.qf.smartplatform.pojo.DefaultRole;
import com.qf.smartplatform.pojo.Role;
import com.qf.smartplatform.pojo.User;
import com.qf.smartplatform.service.UserService;
import com.qf.smartplatform.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2021/12/27 20:36
 * description:
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    private DefaultRoleCache defaultRoleCache;

    private RoleCache roleCache;

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setDefaultRoleCache(DefaultRoleCache defaultRoleCache) {
        this.defaultRoleCache = defaultRoleCache;
    }

    @Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void addUser(User user) {
        Assert.isTrue(!user.isEmpty(CheckType.ADD), () -> {
            throw new AddDataException("必须传递的数据不完整，请检查", ResultCode.USERNAME_NULL);
        });
        List currentUser = userMapper.selectByUserNameOrPhoneOrEmail(user.getUsername(), user.getPhone(), user.getEmail());
        Assert.isTrue(currentUser.size() == 0, () -> {
            throw new AddDataException("账号已存在", ResultCode.DATA_ALREADY_EXIST);
        });
        String password = user.getPassword();
        String sqlt = MD5Utils.getRandomString(15);
        String md5 = MD5Utils.getMd5(password, sqlt, 1024);
        user.setPassword(md5);
        user.setPwdSalt(sqlt);
        user.setCreateTime(new Date());
        user.setCreateBy(user.getUsername());
        DefaultRole defaultRole = defaultRoleCache.getValue("user");
        Role role = roleCache.getValue(defaultRole.getRoleId());
        user.setRemark(role.getRemark());
        userMapper.addUser(user);

    }

    @Override
    public User login(String username, String password) {
        Assert.isTrue(StringUtils.hasText(username)&&StringUtils.hasText(password),()->{
            throw new QueryDataException("帐号或者密码错误", ResultCode.USERNAME_PASSWORD_ERROR);
        });
        User user = userMapper.selectUserByUserName(username);
        Assert.notNull(user,()->{
            throw new QueryDataException("帐号或者密码错误", ResultCode.USERNAME_PASSWORD_ERROR);
        });
        String pwdSalt = user.getPwdSalt();
        String comMd5 = MD5Utils.getMd5(password, pwdSalt, 1024);
        Assert.isTrue(comMd5.equalsIgnoreCase(user.getPassword()),()->{
            throw new QueryDataException("帐号或者密码错误", ResultCode.USERNAME_PASSWORD_ERROR);
        });
        user.setPassword(null);
        user.setPwdSalt(null);
        return user;
    }

    @Override
    public int updateUserLogin(User user) {
        userMapper.upDateUserLogin(user);
        return 0;
    }

    @Override
    public User findUserByUserName(String username) {
        User user = userMapper.selectUserByUserName(username);
        return user;
    }
}
